package com.mys2sh.action;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jofc2.model.Chart;
import jofc2.model.elements.BarChart.Bar;
import jofc2.model.elements.BarChart.Style;
import jofc2.model.elements.LineChart.Dot;
import jofc2.model.elements.BarChart;
import jofc2.model.elements.FilledBarChart;
import jofc2.model.elements.LineChart;
import jofc2.model.elements.PieChart;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class ChartAction extends ActionSupport {
	private int type;
	private Chart ofc;

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public void setOfc(Chart ofc) {
		this.ofc = ofc;
	}

	public Chart getOfc() {
		return ofc;
	}

	// 柱状图
	public Chart genChart() {
		Chart chart = new Chart(new Date().toString()).addElements(
				new FilledBarChart().setOutlineColour("577261").setColour(
						"E2D66A").addValues(9, 8, 7, 6, 5, 4, 3, 2, 1))
				.setBackgroundColour("FFFFFF");
		return chart;
	}

	// 饼状图
	public Chart genPieChart() {

		List<String> labels = new ArrayList<String>();
		List<String> links = new ArrayList<String>();

		int max = 100;
		for (int i = 0; i < 5; i++) {

			labels.add("商品" + (i + 1));
			links.add("chart3.jsp?id=" + i);
		}

		PieChart pieChart = new PieChart();
		pieChart.setText("渠道分布占比\n\n");
		pieChart.setTooltip("他二舅");
		pieChart.addValues(9, 8, 7, 6, 5, 4, 3, 2, 1);
		return new Chart(new Date().toString()).addElements(pieChart);
	}

	// 3d柱状图
	public Chart genBarChart() {
		Bar b1 = new BarChart.Bar(50, "9933CC");
		b1.setTooltip("bar_3d");

		Bar b2 = new BarChart.Bar(50, "0000FF");
		b2.setTooltip("bar_3d");

		int max = 100;
		for (int i = 0; i < 12; i++) {
			b1.setTop(Math.random() * max);
			b1.setBottom(i);
			b2.setTop(Math.random() * max);
			b2.setBottom(i);
		}

		BarChart barChart = new BarChart(BarChart.Style.THREED);
		barChart.setText("渠道运营情况统计图");
		barChart.addBars(b1, b2);

		List<String> labels = new ArrayList<String>();
		labels.add("一月");
		labels.add("二月");
		labels.add("三月");
		labels.add("四月");
		labels.add("五月");
		labels.add("六月");
		labels.add("七月");
		labels.add("八月");
		labels.add("九月");
		labels.add("十月");
		labels.add("十一月");
		labels.add("十二月");

		return new Chart(new Date().toString()).addElements(barChart);
	}

	// 线状图
	public Chart genLineChart() {
		int max = 50;
		Dot dot = new LineChart.Dot(2, "#DC143C", 4, 12);
		Dot dot2 = new LineChart.Dot(2, "#DC143C", 4, 12);

		for (int i = 0; i < 12; i++) {
			dot.setValue(Math.random() * max);
			dot2.setValue(Math.random() * max);
		}

		LineChart lineChart = new LineChart(LineChart.Style.DOT);
		lineChart.setText("全市运营情况统计表");

		lineChart.addDots(dot, dot2);

		lineChart.addValues(9, 8, 7, 6, 5, 4, 3, 2, 1);
		List<String> labels = new ArrayList<String>();
		labels.add("一月");
		labels.add("二月");
		labels.add("三月");
		labels.add("四月");
		labels.add("五月");
		labels.add("六月");
		labels.add("七月");
		labels.add("八月");
		labels.add("九月");
		labels.add("十月");
		labels.add("十一月");
		labels.add("十二月");

		return new Chart(new Date().toString()).addElements(lineChart);

	}

	@Override
	public String execute() throws Exception {

		switch (this.type) {
		case 1:
			setOfc(genChart());
			break;
		case 2:
			setOfc(genPieChart());
			break;
		case 3:
			setOfc(genBarChart());
			break;
		default:
			setOfc(genLineChart());
			break;
		}

		return SUCCESS;
	}

}
